<script setup>
import { ref, reactive, onMounted } from 'vue';
import { ElMessage, ElMessageBox } from 'element-plus';
import { Plus } from '@element-plus/icons-vue'

import * as admin from '@/api/admin.js'
import { useTokenStore } from '@/stores/token.js';
const tokenStore = useTokenStore();

// 模拟数据
const students = ref([]);
const currentPage = ref(1);
const pageSize = ref(5);
const updateModal = ref(false);
const addModal = ref(false);
const total = ref(0);
const tearch_list = ref([]);
const search_name = ref('');
const search_age = ref('');


const updateData = ref({
  id: '',
  parentId: '',
  teachersId: '',
  name: '',
  age: 0,
  gender: '',
  yonghu: '',
  teachers: '',
  img:''
})

const addData = ref({
  parentId: '',
  teachersId: '',
  name: '',
  age: 0,
  gender: '',
  img:''
})

const showDialog = (row) => {
  updateData.value.id = row.id;
  updateData.value.name = row.name;
  updateData.value.age = row.age;
  updateData.value.gender = row.gender;
  updateData.value.yonghu = row.yonghu;
  updateData.value.parentId = row.parentId;
  updateData.value.parentId = row.parentId;
  updateData.value.teachersId = row.teachersId;
  updateData.value.img = row.img;

}

const fetchStudents = async () => {
  const param_user = {
    role: "teacher",
    status: '正常'
  }
  const tearch_data = await admin.findtearchlist(param_user)
  tearch_list.value = tearch_data.data;
  const param = {
    currentPage: currentPage.value,
    name: search_name.value,
    age: search_age.value
  }
  const res = await admin.findStudent(param)
  students.value = res.data;
  total.value = res.total;
  console.log(res);
};

const searchStudents = () => {
  fetchStudents();
};

const deleteStudent = (id) => {
  const param = {
    id: id
  }
  ElMessageBox.confirm('确认删除该学生？', '提示', { type: 'warning' })
    .then(async () => {
      const res = await admin.delStudent(param)
      if (res.code == 0) {
        fetchStudents();
      }
      ElMessage.success('删除成功');
    })
    .catch(() => ElMessage.info('取消删除'));
};

const handlePageChange = (page) => {
  currentPage.value = page;
  fetchStudents();
};


const editMethod = async() => {
  const res = await admin.updateStudent(updateData.value)
  if (res.code == 0) {
    fetchStudents();
  }
  ElMessage.success('修改成功');
  updateModal.value = false;
};

onMounted(() => {
  fetchStudents();
});
</script>

<template>
  <div class="page">
    <el-card class="search-box">
      <el-form :inline="true" class="search-form">
        <el-form-item label="姓名">
          <el-input v-model="search_name" placeholder="请输入学生姓名" clearable />
        </el-form-item>
        <el-form-item label="年龄">
          <el-input v-model="search_age" type="number" placeholder="请输入年龄" clearable />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="searchStudents" icon="el-icon-search">搜索</el-button>
        </el-form-item>
      </el-form>
    </el-card>

    <!-- <el-button type="success" class="add-button" @click="addModal=true">➕ 添加学生</el-button> -->

    <el-table :data="students" border>
      <el-table-column prop="name" label="姓名" />
      <el-table-column prop="age" label="年龄" />
      <el-table-column prop="gender" label="性别" />
      <el-table-column prop="yonghu" label="家长" />
      <el-table-column prop="teachers" label="教师" />
      <el-table-column prop="createdAt" label="创建时间" width="150" />
      <el-table-column label="操作" width="180">
        <template #default="{ row }">
          <el-button type="primary" size="small" @click="showDialog(row),updateModal=true">修改</el-button>
          <el-button type="danger" size="small" @click="deleteStudent(row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-pagination
      :total="total"
      :page-size="pageSize"
      @current-change="handlePageChange"
    />


<el-drawer
  v-model="updateModal"
  title="修改学生信息"
  direction="rtl"
  class="custom-drawer"
  size="40%"
  >
  <div class="demo-drawer__content">
    <el-form 
      ref="updateFormRef" 
      :model="updateData" 
      :rules="rules" 
      label-width="100px"
      style="padding: 10px 20px"
    >
      <!-- 家长信息（不可修改） -->
      <el-form-item label="家长" prop="parentId">
        <el-input v-model="updateData.parentId" disabled />
      </el-form-item>
      <!-- 学生姓名 -->
      <el-form-item label="学生姓名" prop="name">
        <el-input v-model="updateData.name" placeholder="请输入学生姓名" disabled />
      </el-form-item>
      
      <!-- 头像上传（不变） -->
      <el-form-item label="学生头像" prop="img">
        <img v-if="updateData.img" :src="updateData.img" class="avatar" style="width: 200px;height: 200px;" />
      </el-form-item>

      <!-- 选择教师 -->
      <el-form-item label="教师" prop="teachersId">
        <el-select v-model="updateData.teachersId" placeholder="请选择教师">
          <el-option v-for="teacher in tearch_list" :key="teacher.id" :label="teacher.username" :value="teacher.id" />
        </el-select>
      </el-form-item>


      
    </el-form>

    <div class="demo-drawer__footer">
      <el-button @click="updateModal = false">取消</el-button>
      <el-button type="primary"  @click="editMethod">提交</el-button>
    </div>
  </div>
</el-drawer>


<el-dialog 
  v-model="addModal" 
  title="添加学生" 
  width="40%" 
  center 
  class="custom-dialog"
>
  <el-form 
    ref="addFormRef"
    :model="addData" 
    :rules="rules"
    label-width="100px" 
    style="padding: 10px 20px"
  >
    <!-- 选择家长 -->
    <el-form-item label="家长" prop="parentId">
      <el-select v-model="addData.parentId" placeholder="请选择家长">
        <el-option v-for="parent in parentsList" :key="parent.id" :label="parent.name" :value="parent.id" />
      </el-select>
    </el-form-item>

    <!-- 选择教师 -->
    <el-form-item label="教师" prop="teachersId">
      <el-select v-model="addData.teachersId" placeholder="请选择教师">
        <el-option v-for="teacher in teachersList" :key="teacher.id" :label="teacher.name" :value="teacher.id" />
      </el-select>
    </el-form-item>

    <!-- 学生姓名 -->
    <el-form-item label="学生姓名" prop="name">
      <el-input v-model="addData.name" placeholder="请输入学生姓名" clearable />
    </el-form-item>

    <!-- 年龄 -->
    <el-form-item label="年龄" prop="age">
      <el-input-number v-model="addData.age" :min="1" :max="100" style="width: 100%" />
    </el-form-item>

    <!-- 性别 -->
    <el-form-item label="性别" prop="gender">
      <el-radio-group v-model="addData.gender">
        <el-radio label="男">男</el-radio>
        <el-radio label="女">女</el-radio>
      </el-radio-group>
    </el-form-item>

    <!-- 头像上传 -->
    <el-form-item label="学生头像" prop="img">
      <el-upload
        class="avatar-uploader"
        action="https://run.mocky.io/v3/9d059bf9-4660-45f2-925d-ce80ad6c4d15"
        :show-file-list="false"
        :on-success="uploadSuccess"
      >
        <img v-if="addData.img" :src="addData.img" class="avatar" />
        <el-icon v-else class="avatar-uploader-icon"><Plus /></el-icon>
      </el-upload>
    </el-form-item>
  </el-form>

  <template #footer>
    <span class="dialog-footer">
      <el-button @click="addModal = false">取消</el-button>
      <el-button 
        type="primary" 
        :disabled="!isFormValid"
        @click="handleSubmit"
      >
        提交
      </el-button>
    </span>
  </template>
</el-dialog>

  </div>
</template>

<style scoped>
.page {
  padding: 30px;
  background: #f7f7f7;
}

.search-box {
  background: #fff;
  border-radius: 10px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
  padding: 20px;
  margin-bottom: 20px;
}

.add-button {
  background-color: #67c23a;
  color: white;
  border-radius: 8px;
  font-size: 14px;
}

.el-button {
  margin: 0 5px;
}

.el-table {
  margin-top: 20px;
}

.el-pagination {
  margin-top: 20px;
  text-align: center;
}
</style>
